package com.cade.agriculturemall.view.register.registermycode;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.cade.agriculturemall.BuildConfig;
import com.cade.agriculturemall.R;
import com.cade.agriculturemall.base.BaseActivity;
import com.cade.agriculturemall.view.main.MainActivity;

/**
 * Created by Lobby on 2017/11/16.
 */

public class RegisterMyCodeActivity extends BaseActivity<RegisterMyCodeContract.IPresenter> implements RegisterMyCodeContract.IView {
    private Button mBtnCode;
    private String monbilePhone;
    private String passwold;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_mycode_activity);
        monbilePhone=getIntent().getStringExtra("mobilePhoneNumber");
        passwold=getIntent().getStringExtra("password");
        SharedPreferences sharedPreferences = this.getSharedPreferences("Time", Context.MODE_PRIVATE);
        Long time = sharedPreferences.getLong("time", 0L);
        Long timeOffset = System.currentTimeMillis() - time;
        if (timeOffset < 60 * 10 * 1000) {
            cutDown();
        }
    }

    @Override
    public void setupView() {
        final EditText mEtCode= (EditText) findViewById(R.id.mycode_et_code);
        mBtnCode= (Button) findViewById(R.id.mycode_btn_getcode);
        mBtnCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.upCode(monbilePhone,passwold,mEtCode.getText().toString());
            }
        });
    }

    @Override
    public void setupEvent() {

    }

    @Override
    public void setupData() {

    }

    @Override
    public RegisterMyCodeContract.IPresenter setupPresenter() {
        return new RegisterMyCodePresenter(this);
    }

    //倒数
    @Override
    public void cutDown() {
        CountDownTimer timer = new CountDownTimer(60, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                countDownVerifyCodeBtn("还剩" + millisUntilFinished / 1000 + "秒");
            }

            @Override
            public void onFinish() {
            }
        }.start();
    }
//改变button状态
    @Override
    public void countDownVerifyCodeBtn(String time) {
        mBtnCode.setText(time);
        mBtnCode.setClickable(false);
        mBtnCode.setTextColor(getResources().getColor(R.color.countdown));
    }
    @Override
    public void redirection() {
        Intent intent=new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    @Override
    public void ifTryAgain() {

    }

}
